package org.opens.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2022/9/1
 */
public class MyCollectionsUtil {

    /**
     * 均等分切割List
     * @param param 待切割的list
     * @param splitNum  每次切割的数量
     */
    public static <T> List<List<T>> splitList(List<T> param, int splitNum) {
        if (splitNum <= 0) {
            throw new IllegalArgumentException("均等分数不能为null!");
        }

        if (param == null) {
            throw new IllegalArgumentException("待均等分值不能为null!");
        }

        // 计算切割份数
        int len = param.size() / splitNum + (param.size() % splitNum != 0 ? 1 : 0);

        List<List<T>> result = new ArrayList<>(len);

        for (int i = 0; i < len; i++) {
            result.add(param.subList(i * splitNum, Math.min((i + 1) * splitNum, param.size())));
        }

        return result;
    }

    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();

        for (int i = 0; i < 1; i++) {
            arr.add(i);
        }

        List<List<Integer>> res = MyCollectionsUtil.splitList(arr, 20);
        System.out.println(res);
    }

}
